﻿
# ~~~ coding:Utf-8 ~~~

	# ~~ Imports ~~
import es, popuplib, playerlib, usermsg
	# ~~~~~~~~~~~

	# ~~ Variable Script & Version ~~
__version__ = '1.1.1'
es.ServerVar("observation", __version__).makepublic()
	# ~~~~~~~~~~~~~~~~~~~~~~~~

	# ~~ Eventscripts Info ~~
info = es.AddonInfo()
info.name = "observation"
info.version = __version__
info.author = "TiTi and =CmG= Knight (Crappy translator)"
info.url = "http://csswarner.easyforum.fr"
	# ~~~~~~~~~~~~~~~~~~~
	
JoueurObs = 0
obs_joueur = {}
obs_record = {}

def load():
	es.log("Loading Observation Script ...")
	es.regsaycmd("!obs", "observation/liste_observation")
	es.regsaycmd("!observation", "observation/liste_observation")
	es.msg("#multi", "#green[#defaultObservation#green] #default: #lightgreenLoaded")

def unload():
	es.log("Unloading Observation Script ...")
	es.unregsaycmd("!obs")
	es.unregsaycmd("!observation")
	es.msg("#multi", "#green[#defaultObservation#green] #default: #lightgreenUnloaded")

def es_map_start(e):
	global obs_record
	global obs_joueur
	obs_record = {}
	obs_joueur = {}

def player_disconnect(e):
	global obs_joueur
	global obs_record
	userid = int(e["userid"])
	for clef, val in obs_record.items():
		if val == userid:
			f_stop_record(clef)
	for clef, val in obs_joueur.items():
		if val == userid:
			es.tell(clef, "#multi", "#green[#defaultObservation#green] #default: #lightgreenDeactivated#default. The player has disconnected.")
			del(obs_joueur[clef])

def player_team(e):
	global obs_joueur
	userid = int(e["userid"])
	if obs_joueur.has_key(userid):
		Index = es.getplayerteam(userid)
		if Index != 1:
			f_stop_record(userid)
			es.tell(userid, "#multi", "#green[#defaultObservation#green] #default: #lightgreenDeactivated#default.")
			del(obs_joueur[userid])

def player_death(e):
	global obs_joueur
	userid = int(e["userid"])
	for clef in obs_joueur:
		val = obs_joueur[clef]
		if val == userid:
			joueur = playerlib.getPlayer(userid)
			Pseudo = joueur.attributes["name"]
			SteamId = joueur.attributes["steamid"]
			es.tell(clef, "#multi", "#green[#defaultObservation#green] #default: #lightgreenActive#default. The observed player #green%s#default(#lightgreen%s#default) is dead."%(Pseudo, SteamId))

def liste_observation():
	global liste_joueurs
	userid = int(es.getcmduserid())
	Index = es.getplayerteam(userid)
	if Index == 1:
		liste_joueurs = popuplib.easymenu("liste_joueurs", None, observation)
		liste_joueurs.settitle("Choose A Player To Observe:")
		playerlist = es.getUseridList()
		playerlist.remove(userid)
		if obs_joueur.has_key(userid):
			observer = obs_joueur[userid]
			joueur = playerlib.getPlayer(observer)
			Pseudo = joueur.attributes["name"]
			liste_joueurs.addoption(1, "Stop observating %s" %Pseudo)
			playerlist.remove(observer)
		for playerid in playerlist:
			liste_joueurs.addoption(playerid, es.getplayername(playerid))
		liste_joueurs.send(userid)
	else:
		es.tell(userid, "#multi", "#greenYou must be in spectator before you can observe a player")

def observation(userid, choix, popupnom):
	global obs_joueur
	userid = int(userid)
	if choix == 1:
		if obs_record.has_key(userid):
			f_stop_record(userid)
			del(obs_record[userid])
		del(obs_joueur[userid])
		es.tell(userid, "#multi", "#green[#defaultObservation#green] #default: #lightgreenDeactivated#default.")
	else:
		joueur_obs = playerlib.getPlayer(choix)
		Pseudo = joueur_obs.attributes["name"]
		SteamId= joueur_obs.attributes["steamid"]
		obs_joueur[userid] = choix
		JoueurObs = es.getplayerhandle(choix)
		joueur = playerlib.getPlayer(choix)
		Dead = joueur.get("isdead")
		if Dead == 0:
			es.setplayerprop(userid,"CCSPlayer.baseclass.m_hObserverTarget", JoueurObs)
			es.tell(userid, "#multi", "#green[#defaultObservation#green] #default: #lightgreenActivated#default. Observing player: #green%s#default(#lightgreen%s#default)."%(Pseudo, SteamId))
		else:
			es.tell(userid, "#multi", "#green[#defaultObservation#green] #default: #lightgreenActivated#default. The observed player #green%s#default(#lightgreen%s#default) is dead."%(Pseudo, SteamId))

def round_start(e):
	global obs_joueur
	for clef, val in obs_joueur.items():
		joueur = playerlib.getPlayer(val)
		Pseudo = joueur.attributes["name"]
		SteamId = joueur.attributes["steamid"]
		JoueurObs = es.getplayerhandle(val)
		es.setplayerprop(clef,"CCSPlayer.baseclass.m_hObserverTarget", JoueurObs)
		es.tell(clef, "#multi", "#green[#defaultObservation#green] #default: #lightgreenActive#default. Observing player: #green%s#default(#lightgreen%s#default)."%(Pseudo, SteamId))